﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

namespace WP_GasTracker.Converters
{
    public class DateTimeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is DateTime)
            {
                var date = (DateTime)value;
                return parameter == null
                    ? date.ToString(culture)
                    : date.ToString(parameter as string, culture);
            }
            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var valueAsString = value as string;
            return valueAsString != null
                ? (object)DateTime.Parse(valueAsString, culture)
                : null;
        }
    }
}
